package com.dxy.data.webcloud.up2u.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxy.data.webcloud.up2u.Service.RoleService;
import com.dxy.data.webcloud.up2u.dao.RoleMapper;
import com.dxy.data.webcloud.up2u.dao.UserRoleMapper;
import com.dxy.data.webcloud.up2u.domain.RoleDO;
import com.dxy.data.webcloud.up2u.domain.UserRoleDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @CLassName: RoleServiceImpl
 * @Author: weixin
 * @Description: TODO
 * @DATE: 2018/10/8 下午4:59
 * @Version: 1.0
 */
@Transactional
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public RoleDO get(Integer id) {
        RoleDO roleDO = roleMapper.selectById(id);
        return roleDO;
    }

    @Override
    public List<RoleDO> list() {
        List<RoleDO> roles = roleMapper.selectList(new QueryWrapper<RoleDO>());
        return roles;
    }

    @Override
    public List<RoleDO> list(Integer userId) {
        List<UserRoleDO> userRoleDOS = userRoleMapper.selectList(new QueryWrapper<UserRoleDO>().eq("user_id", userId));
        List<RoleDO> roles = new ArrayList<>();
        for (UserRoleDO userRoleDO: userRoleDOS) {
            RoleDO roleDO = roleMapper.selectById(userRoleDO.getRoleId());
            roles.add(roleDO);
        }
        return roles;
    }

}
